﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerThucPhamDao : IThucPhamDao
    {
        public SqlServerThucPhamDao()
        {
            SQLUtility.TableName = "ThucPham";
        }

        //Trả về đối tượng báo ăn
        private ThucPham MakeThucPham(DataRow row)
        {
            Guid _id = Guid.Parse(row["Id"].ToString());
            string _tenThucPham = row["TenThucPham"].ToString();
            Guid _nhomThucPhamId = Guid.Parse(row["NhomThucPhamId"].ToString());
            int _donViTinhId = int.Parse(row["DonViTinhId"].ToString());
            double _giaHienThoi = double.Parse(row["GiaHienThoi"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());

         return new ThucPham(_id, _tenThucPham, _nhomThucPhamId, _donViTinhId, _giaHienThoi, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<ThucPham> MakeThucPhams(DataTable dt)
        {
            IList<ThucPham> list = new List<ThucPham>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeThucPham(row));
            }
            return list;
        }
        private DbParameter[] Take(ThucPham ThucPham)
        {
            DbParameter[] parameters = new DbParameter[6];
            parameters[0] = new SqlParameter("@Id", ThucPham.Id);
            parameters[1] = new SqlParameter("@TenThucPham", ThucPham.TenThucPham);
            parameters[2] = new SqlParameter("@NhomThucPhamId", ThucPham.NhomThucPhamId);
            parameters[3] = new SqlParameter("@DonViTinhId", ThucPham.DonViTinhId);
            parameters[4] = new SqlParameter("@GiaHienThoi", ThucPham.GiaHienThoi);
            parameters[5] = new SqlParameter("@SchoolID", ThucPham.SchoolID);
            
            return parameters;
        }


        public IList<Business_Objects.ThucPham> GetThucPham()
        {
            SQLUtility.TableName = "ThucPham";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeThucPhams(dt);
        }

        public Business_Objects.ThucPham GetThucPhamByID(string ThucPhamID)
        {
            SQLUtility.TableName = "ThucPham";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", ThucPhamID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeThucPham(row);
            }
        }

        public void InsertThucPham(Business_Objects.ThucPham ThucPham)
        {
            SQLUtility.TableName = "ThucPham";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(ThucPham), true);
        }

        public int UpdateThucPham(Business_Objects.ThucPham ThucPham)
        {
            SQLUtility.TableName = "ThucPham";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(ThucPham)); }
            catch { return -1; }
        }

        public int DeleteThucPham(Business_Objects.ThucPham ThucPham)
        {
            SQLUtility.TableName = "ThucPham";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(ThucPham)); }
            catch { return -1; }
        }
    }
}
